package com.cnzz.site1255174697.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.wireless.security.open.datacollection.IDataCollectionComponent;
import com.cnzz.site1255174697.ConstHost;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.activity.WebActivity;
import com.cnzz.site1255174697.activity.WebInfoActivity;
import com.cnzz.site1255174697.activity.WebViewActivity;
import com.cnzz.site1255174697.activity.taobao.KindsListActivity;
import com.cnzz.site1255174697.model.BannerEntity;
import com.cnzz.site1255174697.model.GoodsTagEntity;
import com.cnzz.site1255174697.model.MallTopEntity;
import com.cnzz.site1255174697.utils.AlibcTradeUtils;
import com.cnzz.site1255174697.view.XGridViewForScrollView;
import com.cqyanyu.yanyu.view.XBaseViewHolder;
import com.cqyanyu.yanyu.view.recyclerView.YViewHolder;
import com.cqyanyu.yanyu.view.sliding.XSlidingPlayView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.squareup.picasso.Picasso;

/**
 * 主页顶部
 */
public class MallTopHolder extends YViewHolder<MallTopEntity> {

    protected XSlidingPlayView xSlidingPlayView;//轮播3张
    protected XGridViewForScrollView gridView; //首页分类
    protected XSlidingPlayView tSlidingPlayView;//轮播1张
    protected LinearLayout linFqName;
    protected LinearLayout linFq;
    protected ImageView ivPp;
    protected ImageView ivJx;
    protected TextView tvJx;
    private Context context;
    private TextImgGridAdapter textImgAdapter;
    private MallTopEntity mallTopEntity;
    private GoodsKindsAdapter goodsKindsAdapter;
    private ImageView ivFq;
    private TextView tvKindsName;
    private SimpleDraweeView ivOne;
    private SimpleDraweeView ivTwo;
    private SimpleDraweeView ivThree;
    private ImageView ivPq;
    private TextView tvKindsPp;
    private SimpleDraweeView ivImgOne;
    private SimpleDraweeView ivImgTwo;
    private SimpleDraweeView ivImgThree;
    private LinearLayout lin_fq;
    private LinearLayout lin_pp;
    private LinearLayout lin_fq_name;
    private LinearLayout lin_pp_name;

    public MallTopHolder(View parent) {
        super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mall_top, null));
        context = parent.getContext();
        xSlidingPlayView = (XSlidingPlayView) itemView.findViewById(R.id.xSlidingPlayView);
        gridView = (XGridViewForScrollView) itemView.findViewById(R.id.gridView);
        tSlidingPlayView = (XSlidingPlayView) itemView.findViewById(R.id.tSlidingPlayView);

        //设置原点颜色
        xSlidingPlayView.setNavPageResources(R.drawable.bg_circle_red, R.drawable.xy_circle_red_gray);
        xSlidingPlayView.setNavHorizontalGravity(Gravity.RIGHT);
        xSlidingPlayView.startPlay();

        ivFq = XBaseViewHolder.get(itemView, R.id.iv_fq);
        tvKindsName = XBaseViewHolder.get(itemView, R.id.tv_kinds_name);
        ivOne = XBaseViewHolder.get(itemView, R.id.iv_one);
        ivTwo = XBaseViewHolder.get(itemView, R.id.iv_two);
        ivThree = XBaseViewHolder.get(itemView, R.id.iv_three);
        lin_fq = XBaseViewHolder.get(itemView, R.id.lin_fq);
        lin_fq_name = XBaseViewHolder.get(itemView, R.id.lin_fq_name);
        ivOne.setOnClickListener(this);
        ivTwo.setOnClickListener(this);
        ivThree.setOnClickListener(this);

        ivPq = XBaseViewHolder.get(itemView, R.id.iv_pp);
        tvKindsPp = XBaseViewHolder.get(itemView, R.id.tv_kinds_pp);
        ivImgOne = XBaseViewHolder.get(itemView, R.id.iv_img_one);
        ivImgTwo = XBaseViewHolder.get(itemView, R.id.iv_img_two);
        ivImgThree = XBaseViewHolder.get(itemView, R.id.iv_img_three);
        lin_pp = XBaseViewHolder.get(itemView, R.id.lin_pp);
        lin_pp_name = XBaseViewHolder.get(itemView, R.id.lin_pp_name);

        ivImgOne.setOnClickListener(this);
        ivImgTwo.setOnClickListener(this);
        ivImgThree.setOnClickListener(this);
        ivJx = (ImageView) itemView.findViewById(R.id.iv_jx);
        tvJx = (TextView) itemView.findViewById(R.id.tv_jx);

    }

    @Override
    public void onBindViewHolder(MallTopEntity itemData) {
        super.onBindViewHolder(itemData);
        mallTopEntity = itemData;
        //轮播图(8张)
        xSlidingPlayView.removeAllViews();
        if (mallTopEntity.getAdvsEntities() != null) {
            for (BannerEntity advsEntity : mallTopEntity.getAdvsEntities()) {
                final View mPlayView = LayoutInflater.from(context).inflate(R.layout.xy_play_view_item, null);
                PointF focusPoint = new PointF(0.5f, 0.5f);
                SimpleDraweeView mPlayImage = (SimpleDraweeView) mPlayView.findViewById(R.id.mPlayImage);
                mPlayImage.setImageURI(ConstHost.IMG_BASE_URL + advsEntity.getImg());
                mPlayImage.getHierarchy().setActualImageFocusPoint(focusPoint);
                xSlidingPlayView.addView(mPlayView);
            }
            xSlidingPlayView.setOnItemClickListener(new XSlidingPlayView.AbOnItemClickListener() {
                @Override
                public void onClick(int position) {
                    AlibcTradeUtils.showUrl((Activity) mContext, mallTopEntity.getAdvsEntities().get(position).getUrl());
                }
            });
        }
        //首页分类
        if (mallTopEntity.getKindsEntityList() != null) {
            textImgAdapter = new TextImgGridAdapter(context, mallTopEntity.getKindsEntityList());
            gridView.setAdapter(textImgAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (TextUtils.equals(textImgAdapter.getItem(position).getClass_or_url(), "1")) {
                        context.startActivity(new Intent(context, KindsListActivity.class).putExtra("title", textImgAdapter.getItem(position).getName()).
                                putExtra("type", textImgAdapter.getItem(position).getKey_id()));
                    } else {
                        if (position == 1) {
                            mContext.startActivity(new Intent(mContext, WebViewActivity.class).putExtra("url", textImgAdapter.getItem(position).getUrls()));
                        } else {
//                            AlibcTradeUtils.showUrl((Activity) mContext, textImgAdapter.getItem(position).getUrls());
                            mContext.startActivity(new Intent(mContext, WebInfoActivity.class).putExtra("url", textImgAdapter.getItem(position).getUrls()));
                        }
                    }
                }
            });
        }
        //轮播一张
        tSlidingPlayView.removeAllViews();
        if (mallTopEntity.getBannerEntityList() != null) {
            for (BannerEntity advsEntity : mallTopEntity.getBannerEntityList()) {
                final View mView = LayoutInflater.from(context).inflate(R.layout.xy_play_view_item, null);
                PointF focusPoint = new PointF(0.5f, 0.5f);
                SimpleDraweeView mPlayImage = (SimpleDraweeView) mView.findViewById(R.id.mPlayImage);
                mPlayImage.setImageURI(ConstHost.IMG_BASE_URL + advsEntity.getImg());
                mPlayImage.getHierarchy().setActualImageFocusPoint(focusPoint);
                tSlidingPlayView.addView(mView);
            }
            tSlidingPlayView.setOnItemClickListener(new XSlidingPlayView.AbOnItemClickListener() {
                @Override
                public void onClick(int position) {
                    AlibcTradeUtils.showUrl((Activity) mContext, mallTopEntity.getBannerEntityList().get(position).getUrl());
                }
            });
        }
        //促销，特惠，推荐
        if (mallTopEntity.getKindsTagList() != null) {
            for (final GoodsTagEntity goods : mallTopEntity.getKindsTagList()) {
                if (TextUtils.equals(goods.getName(), "促销疯抢")) {
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goods.getIcon()).into(ivFq);
                    tvKindsName.setText(goods.getName());
                    try {
                        if (goods.getGoods_list() != null && goods.getGoods_list().size() > 0) {
                            PointF focusPoint = new PointF(0.5f, 0.5f);
                            ivOne.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(0).getImg());
                            ivOne.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivTwo.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(1).getImg());
                            ivTwo.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivThree.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(2).getImg());
                            ivThree.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivOne.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(0).getAction_url());
                                }
                            });
                            ivTwo.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(1).getAction_url());
                                }
                            });
                            ivThree.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(2).getAction_url());
                                }
                            });
                        } else {
                            lin_fq.setVisibility(View.GONE);
                            lin_fq_name.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                    }
                } else {
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goods.getIcon()).into(ivPq);
                    tvKindsPp.setText(goods.getName());
                    try {
                        if (goods.getGoods_list() != null && goods.getGoods_list().size() > 0) {
                            PointF focusPoint = new PointF(0.5f, 0.5f);
                            ivImgOne.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(0).getImg());
                            ivImgOne.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivImgTwo.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(1).getImg());
                            ivImgTwo.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivImgThree.setImageURI(ConstHost.IMG_BASE_URL + goods.getGoods_list().get(2).getImg());
                            ivImgThree.getHierarchy().setActualImageFocusPoint(focusPoint);
                            ivImgOne.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(0).getAction_url());
                                }
                            });
                            ivImgTwo.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(1).getAction_url());
                                }
                            });
                            ivImgThree.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    AlibcTradeUtils.showUrl((Activity) mContext, goods.getGoods_list().get(2).getAction_url());
                                }
                            });
                        } else {
                            lin_pp.setVisibility(View.GONE);
                            lin_pp_name.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                    }
                }
            }
        }
        if (mallTopEntity.getTagList() != null) {
            Picasso.with(context).load(ConstHost.IMG_BASE_URL + mallTopEntity.getTagList().get(2).getIcon()).into(ivJx);
            tvJx.setText(mallTopEntity.getTagList().get(2).getName());
        }
    }
}